package com.guangwei.lockscreenforelder.lockscreen

import android.app.KeyguardManager
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import com.guangwei.lockscreenforelder.R
import com.guangwei.lockscreenforelder.util.Constant
import com.guangwei.lockscreenforelder.util.Lunar
import com.guangwei.lockscreenforelder.util.TimeUtil
import kotlinx.android.synthetic.main.activity_lock_screen.*
import java.util.*
import android.os.Build



class LockScreenActivity : AppCompatActivity() {
    private var onGoingTimer:Timer? = null

    companion object {
        /**
         * check whether the screen is locked or not
         */
        var isLocked = false

        fun startLockScreenActivity(context: Context) {
            val lockIntent = Intent(Constant.LOCK_SCREEN_ACTION)
            //lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
            lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
            context.startActivity(lockIntent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        println("LockScreenActivity: onCreate")

        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)

        setContentView(R.layout.activity_lock_screen)

        handleButtons()

        onGoingTimer = Timer()
    }

    override fun onStart() {
        super.onStart()
        initView()
        LockScreenService.startLockScreenService(this)
        isLocked = true
    }

    override fun onDestroy() {
        super.onDestroy()
        println("LockScreenActivity: onDestroy")

        stopTimer()
        LockScreenService.startLockScreenService(this)
    }

    private fun stopTimer() {
        if (onGoingTimer != null) {
            onGoingTimer!!.cancel()
        }
    }

    private fun initView() {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility =
            (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        updateDateTime()

        if (Build.VERSION.SDK_INT >= 26) {
            try {
                val keyguardManager =
                    this@LockScreenActivity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
                keyguardManager.requestDismissKeyguard(
                    this@LockScreenActivity,
                    object : KeyguardManager.KeyguardDismissCallback() {
                        override fun onDismissError() {
                            super.onDismissError()
                        }

                        override fun onDismissSucceeded() {
                            super.onDismissSucceeded()
                        }

                        override fun onDismissCancelled() {
                            super.onDismissCancelled()
                        }
                    })
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }

    }

    private fun updateDateTime() {
        run {
            this.onGoingTimer!!.schedule(object : TimerTask() {
                override fun run() {
                    runOnUiThread(Runnable {
                        var calendar: Calendar = Calendar.getInstance()
                        calendar.time = Date()
                        var lunar:Lunar? = Lunar(calendar)

                        tv_time.text = TimeUtil.time
                        tv_date.text = TimeUtil.curDayChinese + " " + TimeUtil.week + "\n" + "农历 " + (lunar?.lunarString() ?: "")
                    })
                }
            }, 0, 1000)
        }
    }

    private fun handleButtons() {
        btn_unlock_screen.setOnClickListener {
            isLocked = false
            //Toast.makeText(this@LockScreenActivity, "Screen is unlocked", Toast.LENGTH_SHORT).show()
            finish()
        }
    }

}
